﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Timers;
using System.Collections;

namespace pscheck
{
    public partial class ProgramStatusCheck : ServiceBase
    {
        static Timer timer;
        static Hashtable htUrl;
        static Hashtable htIp;
        static Hashtable htPhn;
        static string smsUrl;

        public ProgramStatusCheck()
        {
            InitializeComponent();
            
            timer = new Timer();

            htUrl = new Hashtable();
            htIp = new Hashtable();
            htPhn = new Hashtable();
            smsUrl = string.Empty;
            
            this.EventLog.Source = Constants.EVENT_LOG_NAME;
            this.EventLog.Log = Constants.EVENT_LOG_TYPE;
            if (!EventLog.SourceExists(Constants.EVENT_LOG_NAME))
                EventLog.CreateEventSource(Constants.EVENT_LOG_NAME, Constants.EVENT_LOG_TYPE);

       }

        protected override void OnStart(string[] args)
        {
            List<object> config = Constants.loadConfig(Constants.CONFIG);
            htIp = (Hashtable)config[0]; 
            htUrl = (Hashtable) config[1];
            htPhn = (Hashtable)config[2];
            smsUrl = (string)config[3]; 
            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
            timer.Interval = Constants.INTERVAL;
            timer.Enabled = true;
            check();
        }

        protected override void OnStop()
        {
        }
        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {

            check();
        }

        private static void check()
        {
            List<string> errorIp = null;

            try
            {
                foreach (DictionaryEntry ip in htIp)
                {
                    if (!Constants.ping(ip.Value.ToString()))
                    {
                        if (errorIp == null)
                        {
                            errorIp = new List<string>();
                        }
                        errorIp.Add(ip.Key.ToString());
                    }
                }
                processErrors(errorIp, Constants.ErrorType.PingError);

                errorIp = null;
                foreach (DictionaryEntry ip in htUrl)
                {
                    if (!Constants.getWebPage(ip.Value.ToString()))
                    {
                        if (errorIp == null)
                        {
                            errorIp = new List<string>();
                        }
                        errorIp.Add(ip.Key.ToString());
                    }

                }
                processErrors(errorIp, Constants.ErrorType.Http_Get_Error);
            }
            catch (Exception ex)
            {
                Constants.logMessages(ex.Message, EventLogEntryType.Error);
            }
        }
        private static void processErrors(List<string> list, Constants.ErrorType et)
        {
            if (list!=null)
            {
                string msg = string.Empty;
                foreach (string eIp in list)
                {
                    msg += Constants.builErrorMessage(eIp, et);
                }
                foreach (DictionaryEntry pn in htPhn)
                {
                    Constants.logMessages(msg, EventLogEntryType.Warning);
                    Constants.sendMessage(pn.Key.ToString(), msg, smsUrl);
                }
            }
        }
    }
}

